var eventCtrl = $.mvc.controller.create('event', {
    views: {
        "event_list_tpl": "views/event_list.tpl",
        "event_detail_tpl": "views/event_detail.tpl",
        "cart_tpl": "views/cart.tpl",
        "cart_detail_tpl": "views/cart_detail.tpl"
    },
    /* Array of views to load */
    save: function() {
        value = $("#new-todo").val();
        $("#new-todo").val('');
        var that = this;
        var todo = new Todo();
        if(value.length == 0) return alert("Please enter some text");
        todo.text = value;

        todo.set({
            text: value
        });
        todo.save(function() {
            //var str = "<li><input id='"+todo.id+"' data-id='" + todo.id + "' class='check' type='checkbox' value='active' /><label for='"+todo.id+"'/><div class='todo-text' >" + todo.text + "</div> <span class='todo-destroy' style='display:none;'><input data-id='" + todo.id + "' type='button' class='handleArchive' value='archive' style='width: 90px;height: 43px;' /></span></li>";
            var str=$.template("list_item",{'entry':todo});
            $("#leftTodo ul").append($(str));
            that.updateCounters();
        });


    },
    
    /* This function updates the number of todos on the screen */
    updateCart: function(id, qty) {
        var carts = JSON.parse(localStorage.getItem('shopping_cart'));
         
        var total = 0;
        if (carts && carts.length > 0) {
            for(var i=0; i< carts.length; i++){
                if (carts[i][0].eventpackageID === id) {
                    carts[i][0].cart_qty = qty;
                }
                var amount = Number(carts[i][0].price) * carts[i][0].cart_qty;
                total = total + amount;
            }
        }
        localStorage.setItem('shopping_cart', JSON.stringify(carts));
        $("#cart_div").html($.template('cart_tpl', {
            items: carts
        }));
        var totalFormatted = total.toString().replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1.");
        $("#totalCart").html("Total: Rp. " + totalFormatted);
        $.ui.loadContent("#cart_panel",false,false,"slide");
    },
    
    deleteItem: function(id) {
        var carts = JSON.parse(localStorage.getItem('shopping_cart'));
        
        var total = 0;
        if (carts && carts.length > 0) {
            for(var i=carts.length-1; i>=0 ; i--){
                if (carts[i][0].eventpackageID === id) {
                    carts.splice(i, 1);
                } else {
                    var amount = Number(carts[i][0].price) * carts[i][0].cart_qty;
                    total = total + amount;
                }
            }
        }
        localStorage.setItem('shopping_cart', JSON.stringify(carts));
        $("#cart_div").html($.template('cart_tpl', {
            items: carts
        }));
        var totalFormatted = total.toString().replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1.");
        $("#totalCart").html("Total: Rp. " + totalFormatted);
        $.ui.loadContent("#cart_panel",false,false,"slide");
    },
    detail: function(id) { 
        //$.mvc.route("/garagesale_item");
        //$.ui.loadContent("#item_panel",false,false,"slide");
        //garagesale.fetch(id, function(result){
            
        //});
        
        event1.fetch(id, function(result){
           // alert(result.eventID);
            alert(JSON.stringify(result));
            $.ui.loadContent("#detail_panel",false,false,"slide");
            $('#detail_div').html($.template('event_detail_tpl', {
                event: result
            }));
        });
    },
    detailnya: function(id) { 
        //$.mvc.route("/garagesale_item");
        //$.ui.loadContent("#item_panel",false,false,"slide");
        //garagesale.fetch(id, function(result){ 
        //});
         
        event1.detailnya(id, function(result){
            // alert(result.eventID);
            //alert(JSON.stringify(result));
            // alert('asd');
            
            $.ui.loadContent("#detail_panel",false,false,"slide");
            $('#detail_div').html($.template('event_detail_tpl', {
                detailnya: result
            }));
        }); 
        
    },
    buy: function(id) {
        eventpack.package_detail(id, function(result){ 
             
            addToCart(result);
             
        });
        /*var carts = JSON.parse(localStorage.getItem('shopping_cart'));
        $("#cartList").html($.template('cart_tpl', {
            items: carts
        }));*/
        alert('Added to cart');
    },
    cart: function() {
         
        var carts = JSON.parse(localStorage.getItem('shopping_cart'));
         
         //alert(JSON.stringify(carts));
        
         //alert(carts.length);
        
        $("#cart_div").html($.template('cart_tpl', {
            items: carts
        }));
        
        var total = 0;
        if (carts && carts.length > 0) {
            for(var i=0; i< carts.length; i++){
                var amount = Number(carts[i][0].price) * carts[i][0].cart_qty;
                total = total + amount;
                  
            }
        }
        
        var totalFormatted = total.toString().replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1.");
        $("#totalCart").html("Total: Rp. " + totalFormatted);
        $.ui.loadContent("#cart_panel",false,false,"slide");
        
    },
    cart_detail: function(id) {
         
        var carts = JSON.parse(localStorage.getItem('shopping_cart'));
        if (carts && carts.length > 0) {
            for(var i=0; i< carts.length; i++){
                if(id === carts[i][0].eventpackageID) {
                    $('#cart_detail_div').html($.template('cart_detail_tpl', {
                        item: carts[i]
                    }));
                    $.ui.loadContent("#cart_detail_panel",false,false,"slide");
                }
            }
        }
    },
    default:function() {
        /*$.get("http://192.168.10.22/wnsgbl-repo/mobileapp/mobile_api/events,
            function(obj){
                obj=JSON.parse(obj);
                $("#eventList").html($.template('event_list_tpl', {
                    events: obj
                }));
            }
        );*/ 
        event1.fetchAll(function(all) { 
              alert('asd');
            $("#eventList").html($.template('event_list_tpl', {
                events: all
            }));
        });

    },
    /* This is executed when the controller is created.  It assumes the views are loaded, but can not interact with models
     * This is useful for wiring up page events, etc.
     */
    init: function() {
        
    }
});